//
//  UITextField+Extension.swift
//  joyBao
//
//  Created by Chen on 2017/8/31.
//  Copyright © 2017年 Chen. All rights reserved.
//

import Foundation
import RxCocoa
import RxSwift

extension UITextField {

    func textChangeCallback(_ callback: @escaping (_ text: String) -> ()) {
        self.textChange(0.3, callback: callback)
    }

    /// textField文字改变的回调
    ///
    /// - Parameters:
    ///   - dueTime: 文字改变后用户多久未操作的时间间隔
    ///   - callback: 文字改变后dueTime未操作的回调
    func textChange(_ dueTime: TimeInterval, callback: @escaping (_ text: String) -> ()) {
        self.rx.text.orEmpty.throttle(dueTime, scheduler: MainScheduler.instance).distinctUntilChanged().subscribe({ (event: Event<String>) in
            switch event {
            case .next(let text):
                callback(text)
                break
            default:
                break
            }
        }).addDisposableTo(bag)
    }

}
